import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)


let localStorage = {}
try {
	// 尝试获取localStorage持久存储的数据
	localStorage = uni.getStorageSync('localStorage')
} catch (e) {
	console.log(e)
}
// 永久保存的key名
const localStorageKeys = ['token', 'session_key','real_name','name', 'nickname', 'image','user_image', 'sex', 'user_id', 'mobile', 'id_card',
'phone',
'identity_id',
'username',
'level'
]


// 保存变量到本地存储中
const savelocalStorage = (key, value) => {
	if (localStorageKeys.includes(key)) {
		let tmp = uni.getStorageSync('localStorage') || {};
		tmp[key] = value;
		uni.setStorageSync('localStorage', tmp);
	}
}

let NavBar = {
	height: 44,
	top: 44,
	menuButtonRect: {
		left: 320,
		width: 87
	}
};
// #ifdef MP-WEIXIN
let sysInfo = wx.getSystemInfoSync(); //状态栏的高度
NavBar.statusBarHeight = sysInfo.statusBarHeight;
// 胶囊位置信息
let rect = wx.getMenuButtonBoundingClientRect();
NavBar.menuButtonRect = JSON.parse(JSON.stringify(rect));
// 导航栏高度
let navBarHeight = (rect.top - sysInfo.statusBarHeight) * 2 + rect.height;
NavBar.navBarHeight = navBarHeight;
// 自定义导航栏的高度
NavBar.height = sysInfo.statusBarHeight + navBarHeight;
NavBar.top = 0
NavBar.searchBarH = 88
// #endif

const store = new Vuex.Store({
	state: {
		NavBar,
		baseinfo: localStorage || {},
		config: {},
		yhqList:[],
		checkedItem:{},
		cartNum: 0
	},
	mutations: {
		$setStore(state, data) {
			for (let key in data) {
				state[key] = data[key] || ''
				savelocalStorage(key, data[key])
			}
		},
		set_config(state, data) {
			state.config = data;
		},
		clearCheck(state, data) {
			state.checkedItem = {};
		},
		set_typeList(state, data) {
			state.typeNum = data.length;
			state.typeList = data;
		},
		logout(state, data) {
			uni.clearStorageSync();
			uni.reLaunch({
				url: '/pages/login/login'
			})
		}
	}
})

export default store